/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package wbd_au.objects_mappings;

import com.sun.xml.internal.ws.org.objectweb.asm.Type;
import java.lang.reflect.Field;
import java.sql.Date;
import java.sql.ResultSet;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Gospo
 */
public abstract class ObjectMapping {

    protected Object[] attributes;
    protected String tableName=null;
    
    public String getTableName()
    {
        return tableName;
    }

    public String getAttrSelectList() {
        StringBuilder attrSelList = new StringBuilder();
        attrSelList.append(attributes[0].toString());
        for (int i = 1; i < attributes.length; i++) {
            attrSelList.append(", " + attributes[i].toString());
        }

        return attrSelList.toString();
    }

    public static void addToTable(ObjectMapping objectMapping, JTable table) {
        DefaultTableModel model = (DefaultTableModel) table.getModel();
        model.addRow(objectMapping.getAttrs());
    }

    protected Object[] getAttrs() {
        Class cls = this.getClass();
        Field[] fields = cls.getDeclaredFields();
        Object[] attrsValues = new Object[fields.length];
        for (int i = 0; i < fields.length; i++) {
            try {
                attrsValues[i] = fields[i].get(this);
            } catch (IllegalArgumentException ex) {
                Logger.getLogger(Zolnierz.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                Logger.getLogger(Zolnierz.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        return attrsValues;
    }

    public ObjectMapping read(ResultSet rs) {

        Class cls = this.getClass();
        ObjectMapping objectMapping = null;
        try {
            objectMapping = (ObjectMapping) cls.getConstructor().newInstance();
            Field[] fields = cls.getDeclaredFields();
            for (int i = 0; i < fields.length; i++) {
                Object value = null;
                Field field = fields[i];
                String columnName = attributes[i].toString();
                String fieldType = Type.getType(field.getType()).getClassName();
                String intType = Type.getType(int.class).getClassName();
                String longType = Type.getType(long.class).getClassName();
                String stringType = Type.getType(String.class).getClassName();
                String dateType = Type.getType(Date.class).getClassName();
                if (fieldType.equals(intType)) {
                    value = rs.getInt(columnName);
                } else if (fieldType.equals(longType)) {
                    value = rs.getLong(columnName);
                } else if (fieldType.equals(stringType)) {
                    value = rs.getString(columnName);
                } else if (fieldType.equals(dateType)) {
                    value = rs.getDate(columnName);
                }
                field.set(objectMapping, value);
            }
        } catch (Exception ex) {
            Logger.getLogger(ObjectMapping.class.getName()).log(Level.SEVERE, null, ex);
        }

        return objectMapping;
    }
}
